//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using EntLibContrib.Resource.Configuration;
using EntLibContrib.Resource.Managers;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Providers
{
	/// <summary>
	/// Data Resource Provider.
	/// </summary>
	/// <remarks>
	/// This provider loads an assembly and generates a <see cref="ExtendedComponentResourceManager" /> populated
	/// with resources embedded in the assembly or a satellite based on the resource base name within the assembly.
	/// <para>Note: a <see cref="ExtendedComponentResourceManager" /> can only manage one resource at a time even
	/// if an assembly has multiple resources. Additional Resource Managers will be required for each Resource set
	/// within an assembly.</para>
	/// </remarks>
	[ConfigurationElementType(typeof(DataResourceProviderData))]
	public class DataResourceProvider : ResourceProvider
	{
		#region Fields
		private Database database;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new default instance of the <see cref="DataResourceProvider"/> class.
		/// </summary>
		public DataResourceProvider(Database database, string resourceBaseName)
			: base(resourceBaseName)
		{
			ValidateAndStoreParameters(database);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Create a Resource Manager to manage the resource for an assembly resource provider
		/// </summary>
		/// <returns>
		/// A <see cref="ExtendedComponentResourceManager"/> instance
		/// </returns>
		/// <remarks>
		/// This method makes a type of <see cref="ExtendedComponentResourceManager"/> instance publicly available
		/// based on the database instance and base name retrieved during Initialisation
		/// </remarks>
		public override ExtendedComponentResourceManager CreateResourceManager()
		{
			// Check for first time use of resource manager
			if ( ResourceManager == null )
			{
				// Generate a Resource Manager
				ResourceManager = new DataResourceManager(database, ResourceBaseName);
				ResourceManager.IgnoreCase = true;
			}
			return ResourceManager;
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Validates the and store parameters.
		/// </summary>
		/// <param name="databaseInstance">The database instance.</param>
		private void ValidateAndStoreParameters(Database databaseInstance)
		{
			// check for a null database instance name
			if (databaseInstance == null)
				throw new ArgumentException(InternalResources.ExceptionDatabaseInstanceNull);

			// store the database instance name
			this.database = databaseInstance;
		}
		#endregion
	}
}
